package cn.yanghan.controller;

import cn.yanghan.dto.KillDto;
import cn.yanghan.service.IKillCourseService;
import cn.yanghan.domain.KillCourse;
import cn.yanghan.query.KillCourseQuery;

import cn.yanghan.utils.util.AjaxResult;
import cn.yanghan.utils.util.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 服务接口controller
 * @author yh
 * @since 2022-04-21
 */
@RestController
@RequestMapping("/KillCourse")
public class KillCourseController {

    @Autowired
    private IKillCourseService iKillCourseService;



    /**
     * 秒杀方法
     * @param
     * @return
     */
    @PutMapping("/kill")
    public AjaxResult kill(@RequestBody @Valid KillDto dto){
        String orderNo = iKillCourseService.kill(dto);
        return AjaxResult.me().setResultObj(orderNo);
    }


    /**
     * 查询秒杀列表
     * @param
     * @return
     */
    @PutMapping("/online/list")
    public AjaxResult selectListonline(){
        List<KillCourse> killCourses = iKillCourseService.selectListonline();
        return AjaxResult.me().setResultObj(killCourses);
    }

    /**
     * 查询秒杀列表
     * @param
     * @return
     */
    @PutMapping("/online/one/{id}")
    public AjaxResult selectOneonline(@PathVariable("id") Long id){
        KillCourse killCourse = iKillCourseService.selectOneonline(id);
        return AjaxResult.me().setResultObj(killCourse);
    }


    /**
     * 添加秒杀
     * @param tKillCourse
     * @return
     */
    @PutMapping("/add")
    public AjaxResult add(@RequestBody @Valid KillCourse tKillCourse){
        iKillCourseService.add(tKillCourse);
        return AjaxResult.me();
    }
    /**
     * 保存和修改公用的
     * @param tKillCourse  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody KillCourse tKillCourse){
        try {
            if( tKillCourse.getId()!=null)
                iKillCourseService.updateById(tKillCourse);
            else
                iKillCourseService.insert(tKillCourse);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            iKillCourseService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public KillCourse get(@PathVariable("id")Long id)
    {
        return iKillCourseService.selectById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public List<KillCourse> list(){

        return iKillCourseService.selectList(null);
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<KillCourse> json(@RequestBody KillCourseQuery query)
    {
        Page<KillCourse> page = new Page<KillCourse>(query.getPage(),query.getRows());
        page = iKillCourseService.selectPage(page);
        return new PageList<KillCourse>(page.getTotal(),page.getRecords());
    }
}
